package com.cjack.frenchexcel.service;

import cn.hutool.core.collection.ListUtil;
import com.cjack.frenchexcel.common.Constants;
import com.cjack.frenchexcel.config.ErrorCodeDefine;
import com.cjack.frenchexcel.config.LayPage;
import com.cjack.frenchexcel.config.PrivateConfig;
import com.cjack.frenchexcel.dao.RegisterInfoDao;
import com.cjack.frenchexcel.enumInfo.RoleTypeEnum;
import com.cjack.frenchexcel.enumInfo.UserReviewStatusEnum;
import com.cjack.frenchexcel.model.AdminUserModel;
import com.cjack.frenchexcel.model.RegisterInfoModel;
import com.cjack.frenchexcel.table.AdminUserTable;
import com.cjack.frenchexcel.table.RegisterInfoTable;
import com.cjack.frenchexcel.util.AssertUtil;
import com.cjack.frenchexcel.util.Md5Util;
import com.cjack.frenchexcel.util.ModelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Slf4j
@Service
public class RegisterInfoService {

    @Autowired
    private RegisterInfoDao registerInfoDao;
    @Autowired
    private UserAdminService userAdminService;
    @Autowired
    private RoleService roleService;

    public Page<RegisterInfoModel> list(LayPage page, RegisterInfoModel model) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Specification<RegisterInfoTable> specification = handleCondition(model);
        List<RegisterInfoModel> list = ModelUtils.copyListModel(registerInfoDao.findAll(specification, sort), RegisterInfoModel.class);
        List<RegisterInfoModel> registerInfoModels = ListUtil.page(page.getPage() - 1, page.getLimit(), list);
        for (RegisterInfoModel item : registerInfoModels) {
            if (Objects.nonNull(item.getCompanyId())) {
                AdminUserTable adminUserTable = userAdminService.get(item.getCompanyId());
                item.setBindCompanyName(adminUserTable.getJobNumber());
            }
        }
        Pageable pageable = new PageRequest(page.getPage() - 1, page.getLimit(), sort);
        return new PageImpl<>(registerInfoModels, pageable, list.size());
    }

    private Specification<RegisterInfoTable> handleCondition(RegisterInfoModel model) {
        return null;
    }

    public boolean update(RegisterInfoModel model) {
        RegisterInfoTable registerInfo = registerInfoDao.findOne(model.getId());
        AssertUtil.assertNotNull(registerInfo, ErrorCodeDefine.PARAM_COMMON_ERROR, "数据不存在");
        registerInfo.setCompanyId(model.getCompanyId());
        registerInfoDao.save(registerInfo);
        // 写入账号信息
        AdminUserModel adminUserModel = new AdminUserModel();
        adminUserModel.setCompanyId(model.getCompanyId());
        adminUserModel.setJobNumber(registerInfo.getEmail());
        adminUserModel.setCreateTime(new Date());
        adminUserModel.setPassword(Md5Util.stringToMD5(registerInfo.getPassword()));
        adminUserModel.setPasswordStatus(Constants.USER_CUSTOMER);
        adminUserModel.setReviewStatus(UserReviewStatusEnum.PASS.getCode());
        adminUserModel.setRole(roleService.getByCode(PrivateConfig.EMPLOEE_CODE));
        userAdminService.add(adminUserModel);
        return true;
    }
}
